<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Users
 *
 * This model represents user authentication data. It operates the following tables:
 * - user account data,
 * - user profiles
 *
 * @package	Tank_auth
 * @author	Ilya Konyukhov (http://konyukhov.com/soft/)
 */
class Roles_model extends CI_Model
{
	private $table_name			= 'roles';			// user accounts

	function __construct()
	{
		parent::__construct();

		//$ci =& get_instance();
		//$this->table_name			= $ci->config->item('db_table_prefix', 'tank_auth').$this->table_name;
	}
	
	function create_roles($data){
		$data = array(
					'role' => $this->input->post('role'),
					'role_name' =>  $this->input->post('role_name'),
					'role_parent' => $this->input->post('role_parent'),
					'date_add' => date ('Y-m-d H:i:s')
				);
		$this->db->insert($this->table_name,$data);
	}
	
	function update_roles($id_role){
		$data = array(
					'role' => $this->input->post('role'),
					'role_name' =>  $this->input->post('role_name'),
					'role_parent' => $this->input->post('role_parent'),
					'date_modified' => date ('Y-m-d H:i:s')
				);
		$this->db->where('id', $id_role);
        $this->db->update($this->table_name, $data);
	}
	
	function delete_roles($id_role){
		 $this->db->delete($this->table_name, array('id'=>$id_role));
		 //$this->db->query('delete * from roles, privileges_roles where roles.id = privileges_roles.id and roles.id = "'.$id_role.'"'); 
	}
	
	function delete_priv($id_role){
		$this->db->delete('privileges_roles', array('id'=>$id_role)); 	
	}
	
	function get_roles($id_role){
		$this->db->where('id', $id_role);
		$query = $this->db->get('roles');
			
		$row = $query->row_array();
		return $row['role'];
	}
	
	function getAll($page='', $limit=''){
		if(!empty($limit))
		$this->db->limit($limit,$page);
		
		$query = $this->db->get('roles');
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function select($id_role){
        return $this->db->get_where('roles', array('id'=>$id_role))->row();
    }
	
	function insert_priv_roles($kode){
		$id = $this->db->query("select max(id) as f from roles");
		$id = $id->result();
			foreach($id as $r){
				$ids = $r->f;
			}
				
		$detail = array('id'=> $ids,
						'id_priv' => $kode
        );
			
        $this->db->insert('privileges_roles',$detail);
	}
	
	function update_priv_roles($kode,$id_role){
		$id = $this->db->query("select id as f from roles where id = '".$id_role."'");
		$id = $id->result();
			foreach($id as $r){
				$ids = $r->f;
			}
				
		$detail = array('id'=> $ids,
						'id_priv' => $kode
        );
			
        $this->db->insert('privileges_roles',$detail);
	}
	
	function nm_priv(){
		return $this->db->query('select * from privileges order by id_priv')-> result();
	}
	
	function priv_role(){
		return $this->db->query('select * from privileges_roles')-> result();
	}
	
	function get_priv_role($id_role){
	return $this->db->query("select * from privileges, roles, privileges_roles 
							where roles.id = privileges_roles.id and privileges.id_priv = privileges_roles.id_priv 
							and roles.id = '".$id_role."' ")-> result();
	}
	
	function priv($priv){
		return $this->db->query('select role,priv_name from roles, privileges, privileges_roles where roles.id = privileges_roles.id and privileges.id_priv = privileges_roles.id_priv and privileges.priv_name= "'.$priv.'"')-> result();
	}
}